<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>場地設備管理系統PFMS</title>

<script language="JavaScript">
  function formCheck()
  {
     if(document.form1.box.value == ""){
	      alert("請輸入場地關鍵字");
		  document.form1.box.focus()
		  return false;
	 }
  }
</script>
</head>
<body>
<% if(session.getAttribute("access") == "y" ){%>
<jsp:useBean id="mydb" scope="page" class="bean.ConDbBean"/> <%//連結資料庫的bean %>
<jsp:useBean id="p_state" scope="page" class="bean.StateBean"/>
<table width="750" border="0" align="center">
  <tr>
    <td width="30" rowspan="4"><jsp:include page="../frameset/left.jsp" flush="true" ></jsp:include></td>
    <td colspan="2"><jsp:include page="../frameset/top.jsp" flush="true" ></jsp:include></td>
    <td width="40" rowspan="4"><jsp:include page="../frameset/right.jsp" flush="true" ></jsp:include></td>
  </tr>
  <tr>
    <td rowspan="2"><jsp:include page="../frameset/mean.jsp" flush="true" ></jsp:include>

    </td>
    <td height="32"><jsp:include page="../frameset/search_mean.jsp" flush="true" ></jsp:include></td>
  </tr>
  <tr>
    <td>
    <%--內頁--%>
    <form id="form1" name="form1" method="post" action="placekeywordsearch.jsp">
    <table width="500" border="0" height="200" align="center">
    	<tr align="center">
    		<td>			
    			<table width="403" border="1">
  					<tr>
    					<td width="156">查詢順序</td>
   						<td width="231">欄位名稱</td>
  					</tr>
  					<tr>
    					<td>1</td>
    					<td>
         					 <select name="select" id="select">
       						 <option value="">請選擇</option>
       						 <option value="P_ID">場地編號</option>
       						 <option value="P_NAME">場地名稱</option>
        					 <option value="PS">場地內容</option>
       						</select>
   						</td>
  					</tr>
  					<tr>
    					<td>2</td>
    					<td><select name="select2" id="select2">
    					     <option value="">請選擇</option>
    					     <option value="P_ID">場地編號</option>
     						 <option value="P_NAME">場地名稱</option>
      						 <option value="PS">場地內容</option>
        					</select>
        				</td>
  					</tr>
  					<tr>
    					<td>3</td>
    					<td><select name="select3" id="select3">
    					    <option value="">請選擇</option>
       					    <option value="P_ID">場地編號</option>
      						<option value="P_NAME">場地名稱</option>
      						<option value="PS">場地內容</option>
       						 </select>
       					</td>
  					</tr>
				</table>
    		</td>
    	</tr>
  		<tr align="center">
    		<td>
      		場地關鍵字搜尋
      <input type="text" name="box"/>
      <input type="submit" name="Submit" value="搜尋" onClick="return formCheck()" value="搜尋"/>
         
      <%
	  if(request.getParameter("box")==null){
	   //out.println("box");
	  }
	  else{
	  String search_box = new String(request.getParameter("box").getBytes("iso-8859-1"),"utf-8");
      String select1 = new String(request.getParameter("select").getBytes("iso-8859-1"),"utf-8");
      String select2 = new String(request.getParameter("select2").getBytes("iso-8859-1"),"utf-8");
      String select3 = new String(request.getParameter("select3").getBytes("iso-8859-1"),"utf-8");
      
     String sql="select COUNT(*) from PLACE WHERE P_ID LIKE '%"+search_box+"%' OR P_NAME LIKE '%"+search_box+"%' OR PS LIKE '%"+search_box+"%'";
      //找到所有的筆數
      ResultSet rs2=mydb.executeQuery(sql);
	  rs2.next();
	  
	  int num=java.lang.Integer.valueOf(rs2.getString(""));
	  if(num==0){
	   out.print("搜尋不到");
	  }
	  else{
	  %>
	  
	  <table width="500" border="1" align="center">
  		<tr>
    		<td>場地編號</td>
    		<td>場地名稱</td>
    		<td>場地內容</td>
  		</tr>
	  <!-- 輸出第一順位 -->
	  <%
	  String sql2="select * from PLACE WHERE "+select1+" LIKE '%"+search_box+"%'";
	  ResultSet rs=mydb.executeQuery(sql2);
	  %>
	  
	  <%
	  if(rs.next()){
	  	do{
	 	
	   %>
	 	 <tr>
	  	<td><a href="../PFManage/detailPlace.jsp?id=<%=rs.getString("P_ID")%>" target="_black" class="style2"><%=rs.getString("P_ID")%></a></td>
	  	<td><%=rs.getString("P_NAME")%></td>
	  	<td><%=rs.getString("PS")%></td>
	  	
  		</tr>

  		<%}while(rs.next());
	  }
	   %>
  		 <!-- 結束輸出第一順位 -->
      <!-- 輸出第二順位 -->
	  <%
	  String sqlselect2="select * from PLACE WHERE "+select2+" LIKE '%"+search_box+"%' and "+select1+" NOT LIKE '%"+search_box+"%'";
	  ResultSet rs3=mydb.executeQuery(sqlselect2);
	  %>
	  
	  <%
	  if(rs3.next()){
	  	do{
	 	
	   %>
	 		 <tr>
	  		<td><a href="../PFManage/detailPlace.jsp?id=<%=rs3.getString("P_ID")%>" target="_black" class="style2"><%=rs3.getString("P_ID")%></a></td>
	  		<td><%=rs3.getString("P_NAME")%></td>
	  		<td><%=rs3.getString("PS")%></td>
	  	
  			</tr>

  		<%}while(rs3.next());
	  }
	   %>
  		 <!-- 結束輸出第二順位 -->
  		<!-- 輸出第三順位 -->
	  <%
	  String sqlselect3="select * from PLACE WHERE "+select3+" LIKE '%"+search_box+"%' and "+select2+" NOT LIKE '%"+search_box+"%' and "+select1+" NOT LIKE '%"+search_box+"%'";
	  ResultSet rs4=mydb.executeQuery(sqlselect3);
	  %>
	  
	  <%
	  if(rs4.next()){
	  do{
	 	
	   %>
	 	 <tr>
	  	<td><a href="../PFManage/detailPlace.jsp?id=<%=rs4.getString("P_ID")%>" target="_black" class="style2"><%=rs4.getString("P_ID")%></a></td>
	  	<td><%=rs4.getString("P_NAME")%></td>
	  	<td><%=rs4.getString("PS")%></td>
	  	
  		</tr>

  		<%}while(rs4.next());
	  }
	   %>
  		 <!-- 結束輸出第三順位 --> 
  		 
	  </table>
	
	<%  }
	  %>
	  
	 <%}
	  
	  %>
    
    </td>
  </tr>
</table></form>
<%--內頁end--%>
    </td>
  </tr>
  <tr>
    <td colspan="2"></td>
  </tr>
</table>
<%
  }
  else{
  	response.sendRedirect("../index.jsp"); //若沒有登入則回到index.jsp
  } 
  %>
</body>
</html>
